package Dictionary;

import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XModel;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.view.XSelectionSupplier;
import java.io.UnsupportedEncodingException;

/**
 * This class provide the method which is needed to get a selected
 * word from Open Office
 * @author SUMEDHA
 */
public class AutomaticDictionary {

    private final XComponentContext m_xContext;

    public AutomaticDictionary(XComponentContext m_xContext) {
        this.m_xContext = m_xContext;
    }

    /**
     * Returns the selected word from Open Office.
     * <br>Reference: <a href="http://www.koders.com/java/fid71CACAE4C3A5E8BDB3280976007CB0676E7DD2BD.aspx?s=mcall%3Astring"target="_blank">WriterSelector </a>
     * </br>
     * <br>Open Office Developer's Guide: Chapter 8</br>
     * @return The word being selected
     * @throws UnsupportedEncodingException
     */
    public String getSelectedText() throws UnsupportedEncodingException {
        String selectedWord = null;
        // Get the remote office service manager
        XMultiComponentFactory xmcf = m_xContext.getServiceManager();

        try {
            // get the Desktop service
            Object desktop = xmcf.createInstanceWithContext("com.sun.star."
                    + "frame.Desktop", m_xContext);

            // query its XDesktop interface, we need the current component
            XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);

            // retrieve the current component and access the controller
            XComponent xCurrentComponent = xDesktop.getCurrentComponent();

            // get the XModel interface from the component
            XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xCurrentComponent);

            // the model knows its controller
            XSelectionSupplier xSelectionSupplier = (XSelectionSupplier) UnoRuntime.queryInterface(XSelectionSupplier.class, xModel.getCurrentController());
            Object oSelection = xSelectionSupplier.getSelection();

            com.sun.star.container.XIndexAccess xIndexAccess =
                    (com.sun.star.container.XIndexAccess) UnoRuntime.queryInterface(
                    com.sun.star.container.XIndexAccess.class, oSelection);
            com.sun.star.text.XTextRange xTextRange = null;
            int count = xIndexAccess.getCount();
            for (int i = 0; i < count; i++) {

                xTextRange = (com.sun.star.text.XTextRange) UnoRuntime.queryInterface(
                        com.sun.star.text.XTextRange.class,
                        xIndexAccess.getByIndex(i));
                selectedWord = xTextRange.getString();
            }

        } catch (Exception e) {
            System.out.println("Error:" + e.getMessage());
        }
        selectedWord = convertWordToUnicode(selectedWord);
        return selectedWord;

    }

    /**
     * Convert a given word which is in a non_unicode format in to Unicode format
     * @param  word       The word to be converted.
     * @return  Unicode Word.
     * @throws UnsupportedEncodingException
     */
    public String convertWordToUnicode(String word) throws UnsupportedEncodingException {
        String unicodeWord = null;
        byte[] utf8Bytes = word.getBytes("UTF8");
        unicodeWord = new String(utf8Bytes, "UTF8");
        return unicodeWord;
    }
}
